package top.iridescent.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import top.iridescent.util.BeanUtil;
import top.iridescent.util.ObjectUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

@Data
@TableName("mh_base_attribute")
public class MhBaseAttribute implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableField(exist = false)
    private int total = this.li + this.mo + this.nai + this.ti + this.min;
    @TableField(exist = false)
    private Long roleId;

    /**
     * =========================================================
     */

    @TableId
    private Long id;
    private int li = 0;
    private int mo = 0;
    private int nai = 0;
    private int ti = 0;
    private int min = 0;
    private String type;

    public void addLevelAttr(int level) {
        this.li = this.li + level;
        this.mo = this.mo + level;
        this.nai = this.nai + level;
        this.ti = this.ti + level;
        this.min = this.min + level;
        handleTotal();
    }
    
    public void add(MhBaseAttribute baseAttribute) {
        if (ObjectUtils.isEmpty(baseAttribute)) {
            return;
        }
        this.li = this.li + baseAttribute.getLi();
        this.mo = this.mo + baseAttribute.getMo();
        this.nai = this.nai + baseAttribute.getNai();
        this.ti = this.ti + baseAttribute.getTi();
        this.min = this.min + baseAttribute.getMin();
        handleTotal();
    }

    public void handleTotal() {
        total = this.li + this.mo + this.nai + this.ti + this.min;
    }

    public MhBaseAttribute clone() {
        MhBaseAttribute rr = new MhBaseAttribute();
        BeanUtil.copyProperties(this, rr);
        return rr;
    }

    public String getProportion() {
        if (total == 0) {
            return "0 : 0 : 0 : 0 : 0";
        }
        BigDecimal tiP = new BigDecimal(ti).multiply(new BigDecimal(5)).divide(new BigDecimal(total), 1, RoundingMode.HALF_UP);
        BigDecimal naiP = new BigDecimal(nai).multiply(new BigDecimal(5)).divide(new BigDecimal(total), 1, RoundingMode.HALF_UP);
        BigDecimal liP = new BigDecimal(li).multiply(new BigDecimal(5)).divide(new BigDecimal(total), 1, RoundingMode.HALF_UP);
        BigDecimal moP = new BigDecimal(mo).multiply(new BigDecimal(5)).divide(new BigDecimal(total), 1, RoundingMode.HALF_UP);
        BigDecimal minP = new BigDecimal(min).multiply(new BigDecimal(5)).divide(new BigDecimal(total), 1, RoundingMode.HALF_UP);
        StringBuffer a = new StringBuffer();
        StringBuffer b = new StringBuffer();
        StringBuffer c = new StringBuffer();
        if (ti != 0) {
            a.append("体 : ");
            b.append(tiP + " : ");
            c.append(ti + " : ");
        }
        if (nai != 0) {
            a.append("耐 : ");
            b.append(naiP + " : ");
            c.append(nai + " : ");
        }
        if (min != 0) {
            a.append("敏 : ");
            b.append(minP + " : ");
            c.append(min + " : ");
        }
        if (li != 0) {
            a.append("力 : ");
            b.append(liP + " : ");
            c.append(li + " : ");
        }
        if (mo != 0) {
            a.append("魔 : ");
            b.append(moP + " : ");
            c.append(mo + " : ");
        }
        a.append("d");
        b.append("d");
        c.append("d");
        return a.toString().replaceAll(" : d", "") + "==" +
                b.toString().replaceAll(" : d", "") + "==" +
                c.toString().replaceAll(" : d", "");
    }
}
